package com.example.chirdrenserver.controller.AdminController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.chirdrenserver.Authority.Authority;
import com.example.chirdrenserver.Pojo.dto.BasePage;
import com.example.chirdrenserver.Pojo.dto.PermissionDTO;
import com.example.chirdrenserver.Pojo.dto.RolePermissionDTO;
import com.example.chirdrenserver.Pojo.entity.Role;
import com.example.chirdrenserver.Pojo.entity.RolePermission;
import com.example.chirdrenserver.Pojo.vo.RolePermissionVO;
import com.example.chirdrenserver.Service.AdminService.RolePermissionService;
import com.example.chirdrenserver.Service.AdminService.RoleService;
import com.example.chirdrenserver.Utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Collection;


/**
 * 角色管理
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private  RolePermissionService rolePermissionService;

    /**
     * 添加角色
     *
     * @param role
     * @return
     */
    @PostMapping("/add")
    @Authority(authority= "permission:role")
    public Result add(@RequestBody Role role) {
        roleService.add(role);
        return Result.success();
    }

    /**
     * 修改角色
     *
     * @param role
     * @return
     */
    @PutMapping
    @Authority(authority= "permission:role")
    public Result update(@RequestBody Role role) {
       roleService.updateById(role);
        return Result.success();
    }

    /**
     * 删除角色
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @Authority(authority= "permission:role")
    public Result delete(@PathVariable String id) {
        roleService.removeRole(id);
        return Result.success();
    }

    /**
     * 获取所有角色权限
     *
     * @param
     * @return
     */
    @PostMapping
    @Authority(authority= "permission:role")
    public Result<Collection> getPermission(@RequestBody BasePage basePage) {
        Collection<RolePermissionVO> rolePermissionVOs = roleService.listRolePermission(basePage);
        return Result.success(rolePermissionVOs);
    }

    /**
     * 根据id获得数据
     */
    @GetMapping("/{id}")
    @Authority(authority= "permission:role")
    public Result getRoleByRoleId(@PathVariable String id) {
        roleService.getById(id);
        return Result.success();
    }
    @PostMapping("list/role")
    @Authority(authority= "permission:role")
    public Result getRoleList() {
        return Result.success(  roleService.list());
    }
    /**
     * 删除角色权限
     *
     * @param
     * @return
     */
    @DeleteMapping("/permission")
    @Authority(authority= "permission:role")
    public Result deletePermission(@RequestBody RolePermissionDTO rolePermissionDTO) {
        rolePermissionService.remove(new LambdaQueryWrapper<RolePermission>().eq(RolePermission::getPermissionId,rolePermissionDTO.getPermissionId()).eq(RolePermission::getRoleId,rolePermissionDTO.getRoleId()));
        return Result.success();
    }
    @PostMapping("/permission")
    @Authority(authority= "permission:role")
    public Result addPermission(@RequestBody RolePermissionDTO rolePermissionDTO) {
        RolePermission rolePermission = new RolePermission();
        rolePermission.setPermissionId(rolePermissionDTO.getPermissionId());
        rolePermission.setRoleId(rolePermissionDTO.getRoleId());
        rolePermissionService.save(rolePermission);
        return Result.success();
    }
}

